package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.controller.BankAccountInfoController;
import com.example.dangerouscargodriver.entry.BankAccount;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.example.dangerouscargodriver.R;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class ShowHtmlActivity extends HttpRequestActivity {

    private static final String TAG = "ShowHtmlActivity";
    private ActionBar mActionBar = null;

    private WebView mHelpWebView = null;

    private String mType = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(ShowHtmlActivity.class);
        setContentView(R.layout.activity_show_html);

        mType = getIntent().getStringExtra("type");

        mHelpWebView = (WebView) findViewById(R.id.webview_show_html);
        WebSettings webSettings = mHelpWebView.getSettings();
        webSettings.setLoadWithOverviewMode(true);
        //缩放开关
        webSettings.setSupportZoom(false);
        //设置此属性，仅支持双击缩放，不支持触摸缩放（在android4.0是这样，其他平台没试过）
        // 设置是否可缩放
        webSettings.setBuiltInZoomControls(false);
        mHelpWebView.setHorizontalScrollBarEnabled(false);

        mActionBar = findViewById(R.id.actionBarShowHtml);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        executeGetAgreement();
    }

    private void executeGetAgreement(){
        HashMap<String, String> param = new HashMap<String, String>();
        if(mType != null) {
            param.put("type", mType);
        }

        sendHttpRequest(
                IHttpRequest.RequestMethod.GET_METHOD,
                RemoteAPI.REQUEST_V1_AGREEMENT,
                null,
                param,
                RemoteAPICmd.REQUEST_V1_AGREEMENT);
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V1_AGREEMENT){
                Log.d(TAG, "onResponse: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    mHelpWebView.loadData(res.getJSONObject("data").getString("agreement"),
                            "text/html",
                            null);
                }
                if(responseInfo != null && responseInfo.getStatus() == 0){
                }
            }
            else{

            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {

    }
}